
package hasthi.manager;

import hasthi.common.HasthiException;
import hasthi.manager.AssignResourcesDocument.AssignResources;
import hasthi.manager.AssignResourcesDocument.AssignResources.ResourceData;
import hasthi.manager.ElectedNoticeDocument.ElectedNotice;
import hasthi.manager.GetResourcesDocument.GetResources;
import hasthi.manager.NewCoordinatorInvitationDocument.NewCoordinatorInvitation;
import hasthi.manager.NominationRequestDocument.NominationRequest;
import hasthi.manager.UnAssignResourceDocument.UnAssignResource;

import javax.xml.namespace.QName;

public class ManagerClientUtils {

    public static NominationRequestDocument createNominationRequest(ManagerAddress oldCoordinator,long oldCoordinatorTimestamp){
        NominationRequestDocument requestDocument = NominationRequestDocument.Factory.newInstance();
        NominationRequest nominationRequest = requestDocument.addNewNominationRequest();
        nominationRequest.setOldCoordinatorAddress(oldCoordinator);
        nominationRequest.setOldCoordinatorTimestamp(oldCoordinatorTimestamp);
        return requestDocument;
  }
  
  public static ElectedNoticeDocument createElectedNotice(ManagerAddress address,long coordinatorTimestamp){
      ElectedNoticeDocument document = ElectedNoticeDocument.Factory.newInstance();
      ElectedNotice electedNotice = document.addNewElectedNotice();
      electedNotice.setAddress(address);
      electedNotice.setCoordinatorTimestamp(coordinatorTimestamp);
      return document;
  }
  
  public static GetCoordinatorDocument getCoordinator(){
      GetCoordinatorDocument document = GetCoordinatorDocument.Factory.newInstance();
      document.addNewGetCoordinator();
      return document;
  }
  
  
  public static AssignResourcesDocument createAssignResourceRequest(String managmentEndpoint,QName resourceName) throws HasthiException{
      AssignResourcesDocument document = AssignResourcesDocument.Factory.newInstance();
      AssignResources assignResources = document.addNewAssignResources();
      ResourceData resource = assignResources.addNewResourceData();
      resource.setManagementEndpoint(managmentEndpoint);
      resource.setName(resourceName);
      return document;
  }
  
  public static UnAssignResourceDocument createUnassignResourceRequest(String managmentEndpoint,QName resourceName) throws HasthiException{
      UnAssignResourceDocument document = UnAssignResourceDocument.Factory.newInstance();
      UnAssignResource resource = document.addNewUnAssignResource();
      resource.setManagementEndpoint(managmentEndpoint);
      resource.setName(resourceName);
      return document;
  }
  
  public static GetResourcesDocument createGetResourcesRequest(QName[] resources,boolean summerized){
      GetResourcesDocument requestDocument = GetResourcesDocument.Factory.newInstance();
      GetResources getResources = requestDocument.addNewGetResources();
      
      getResources.setResourceNameArray(resources);
      getResources.setSummerized(summerized);
      
      return requestDocument;
  }
  
  public static ListAssignedResourcesDocument createListResourcesRequest(){
      ListAssignedResourcesDocument document = ListAssignedResourcesDocument.Factory.newInstance();
      document.addNewListAssignedResources();
      return document;
  }

    public static NewCoordinatorInvitationDocument createNewCoordinatiorInvitation(ManagerAddress oldCoordinatorAddress,long oldCoordinatorTimestamp) {
        NewCoordinatorInvitationDocument document = NewCoordinatorInvitationDocument.Factory.newInstance();
        NewCoordinatorInvitation newCoordinatorInvitation = document.addNewNewCoordinatorInvitation();
        if(oldCoordinatorAddress != null){
            newCoordinatorInvitation.setOldCoordinatorAddress(oldCoordinatorAddress);    
        }
        if(oldCoordinatorTimestamp > 0){
            newCoordinatorInvitation.setOldCoordinatorTimestamp(oldCoordinatorTimestamp);    
        }
        return document;
        
    } 
}

